<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$plugin_name =$this->pluginname;
$filename=$_SESSION['role_id']==2?'storeid':'workerid';
$filevalue=$_SESSION['role_id']==2?$this->store_id:$this->worker_id;
if($_RQ['op']=='list'){
	if($_RQ['id']){
		$listid=intval($_RQ['id']);
		$this->session('listid',$listid);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array($filename=>$filevalue,'pluginname'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']);
	if($listid){
		$params['listid']=$listid;
		$return['params']['listid']=$listid;
	}
	//批号
	if(!empty($_RQ['code'])){
		$params['code']=intval($_RQ['code']);
		$return['params']['code']=$_RQ['code'];
	}
	//备注
	if(!empty($_RQ['remark'])){
		$params['remark']='%'.trim($_RQ['remark']).'%';
		$return['params']['remark']=$_RQ['remark'];
	}
    $fields='';
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_kabao_batch',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$item['listtitle']=pdo_getcolumn('jy_kabao_list',array('id'=>$item['listid']),'title');
		$item['member']=cfc('core')->member_show($item['qumid']);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('1'=>'正常','0'=>'失效');		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'批次管理'),
		),
		'list'=>array(
		'tip'=>'卡包的<em>业务逻辑</em>：</br>
				1、每个卡包，生成一批次，卡包码；生成卡包码时可以绑定大夫</br>
				2、找印刷商，每行生成一个二维码，批量印刷二维码（<em>温馨提示</em>：下载的只是字符串/链接地址。印刷商，会帮你转成二维码），如果直接下载二维码，文件太大，没什么意义。；</br>
				3、每个二维码印刷一个卡片',
		'data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'listtitle','title'=>'所属卡包','type'=>'string'),
			array('name'=>'member','title'=>'分配渠道商','type'=>'member'),
			array('name'=>'num','title'=>'卡包码数量','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'batchtypes'),
			array('type'=>'select','name'=>'storeid','title'=>'绑定医馆','options'=>'stores','show'=>array('type'=>array(1,2))),
			array('type'=>'select','name'=>'workerid','title'=>'绑定大夫','options'=>'workers','show'=>array('type'=>2),'option_title'=>'name'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		'action'=>array(
			array('type'=>'href','op'=>'setqumid','title'=>'设置渠道商'),
			array('type'=>'post','op'=>'down_code','title'=>'下载'),
			array('type'=>'href','op'=>'codelist','title'=>'查看卡包码'),
		)
		),
		'search'=>array(			
			array('name'=>'listid','width'=>15,'title'=>'所属卡包ID','type'=>'choose','op'=>'choose_goods'),
			array('name'=>'code','width'=>15,'title'=>'批号','type'=>'string'),
			array('name'=>'remark','width'=>15,'title'=>'备注','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$return['batchtypes']=$this->batchtypes;
	$return['kabaos']=pdo_getall('jy_kabao_list',array('pluginname'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['stores']=pdo_getall('jy_zhongyi_store',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['workers']=pdo_getall('jy_zhongyi_worker',array('uniacid'=>$_SESSION['uniacid']),array('name','id'),'id');
	exi($return);
}
if($_RQ['op']=='codelist'){
	if($_RQ['id']){
		$batchid=intval($_RQ['id']);
		$this->session('batchid',$batchid);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array($filename=>$filevalue,'pluginname'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']);
	if($batchid){
		$params['batchid']=$batchid;
		$return['params']['batchid']=$batchid;
	}
	if(!empty($_RQ['type'])){
		$params['type']=trim($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if(!empty($_RQ['listid'])){
		$params['listid']=trim($_RQ['listid']);
		$return['params']['listid']=$_RQ['listid'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=trim($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=trim($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['sn'])){
		$params['sn']='%'.trim($_RQ['sn']).'%';
		$return['params']['sn']=$_RQ['sn'];
	}
    $fields='';
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_kabao_batch_code',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['listtitle']=pdo_getcolumn('jy_kabao_list',array('id'=>$item['listid']),'title');
		$item['url']=custom_url('jy_kabao/item//code='.$item['sn'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['codestatus']=$this->codestatus;		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'codelist','title'=>'卡码列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'codedel','del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'listtitle','title'=>'所属卡包','type'=>'string'),
			array('name'=>'sn','title'=>'编号','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'batchtypes'),
			array('type'=>'select','name'=>'storeid','title'=>'绑定医馆','options'=>'stores'),
			array('type'=>'select','name'=>'workerid','title'=>'绑定大夫','options'=>'workers','option_title'=>'name'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'链接','type'=>'copy'),
			array('name'=>'member','title'=>'领卡人','type'=>'member'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'codestatus'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),
		),
		'search'=>array(
			array('name'=>'type','width'=>15,'title'=>'类型','type'=>'radio','options'=>'batchtypes'),
			array('type'=>'select','width'=>15,'name'=>'listid','title'=>'所属卡包','options'=>'kabaos'),
			array('type'=>'select','width'=>15,'name'=>'storeid','title'=>'绑定医馆','options'=>'stores'),
			array('type'=>'select','width'=>15,'name'=>'workerid','title'=>'绑定大夫','options'=>'workers','option_title'=>'name'),
			array('name'=>'sn','width'=>15,'title'=>'编号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'codestatus'),
		)
	);
	$return['batchtypes']=$this->batchtypes;
	$return['kabaos']=pdo_getall('jy_kabao_list',array('pluginname'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['stores']=pdo_getall('jy_zhongyi_store',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['workers']=pdo_getall('jy_zhongyi_worker',array('uniacid'=>$_SESSION['uniacid']),array('name','id'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setqumid'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		pdo_update('jy_kabao_batch',array('qumid'=>$post['qumid'],'qufee'=>$post['qufee']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('修改成功！','','list');
	}
	$item=pdo_get('jy_kabao_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])),array('id','qufee','qumid'));
	if(!$item){
		exi('指定卡不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改渠道',
		'edit'=>array(
			array('type'=>'choose','name'=>'qumid','title'=>'设置渠道人','action'=>'core/member.get_member/'),
			array('type'=>'string','name'=>'qufee','title'=>'设置佣金','help'=>'线下统计结算给渠道商'),
		),
	);
	exi($return);
}
if($_RQ['op']=='down_code'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择批次','error','list');	
	}
	$file=pdo_getcolumn('jy_kabao_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'code_file');
	$file=tomedia($file);
	exi('','','',$file);
}
if($_RQ['op']=='del'){
	exi('不能删除！','error');
}
if($_RQ['op']=='codedel'){
	exi('不能删除！','error');
}
?>